<template>
  <a-form :rules="configRules" :model="configFormConfig.form" @finish="submit" :label-col="{ span: 6 }"
    :wrapper-col="{ offset: 1 }" label-align="right" ref="configFormRef" :disabled="hasPerm('system:config:edit')">
    <FormCard title="编辑/新增" :back="back" subtitle="参数配置">
      <template #extra>
        <a-button htmlType="submit" type="primary" :loading="configTable.loading" block>
          保存
        </a-button>
      </template>
      <a-row :gutter="8">
        <a-col :span="16">
          <a-card>
            <a-form-item label="参数名称" name="configName">
              <a-input allow-clear placeholder="请输入参数名称" v-model:value="configFormConfig.form.configName">
              </a-input>
            </a-form-item>
            <a-form-item label="参数键名" name="configKey">
              <a-input allow-clear placeholder="请输入参数键名" v-model:value="configFormConfig.form.configKey">
              </a-input>
            </a-form-item>
            <a-form-item label="参数键值" name="configValue">
              <a-textarea allow-clear placeholder="请输入参数键值"
                v-model:value="configFormConfig.form.configValue"></a-textarea>
            </a-form-item>
            <a-form-item label="系统内置" name="configType">
              <a-input allow-clear placeholder="请输入系统内置（Y是 N否）" v-model:value="configFormConfig.form.configType">
              </a-input>
            </a-form-item>
            <a-form-item label="备注" name="remark">
              <a-textarea allow-clear placeholder="请输入备注" v-model:value="configFormConfig.form.remark"></a-textarea>
            </a-form-item>
          </a-card>
        </a-col>
        <AxExtra></AxExtra>
      </a-row>
    </FormCard>
  </a-form>
</template>

<script setup lang="ts">
import {
} from '../data/options';
import { hasPerm } from '@/utils/common/perm';
import type { Response } from '@/api/config/types';
import {
  createConfig,
  updateConfig,
} from '@/api/modules/system/config/config';
import { notify, } from '@/utils/table/table';
import { configList } from '../data/curd';
import {
  configFormConfig,
  configRules,
  configFormRef
} from '../data/form';
import { configTable } from '../data/table';
import AxExtra from './AxExtra.vue';

const submit = async () => {
  configTable.value.loading = true;
  let result: undefined | Response<any> = undefined
  if (configFormConfig.value.form.configId) {
    const { data } = await updateConfig(configFormConfig.value.form)
    result = data
  } else {
    const { data } = await createConfig(configFormConfig.value.form)
    result = data
  }
  notify(result)
  configTable.value.loading = false;
  await configList();
  back();

};

const back = () => {
  configFormConfig.value.display = false;
  configFormConfig.value.reset!();
};

</script>

<style lang="scss" scoped></style>
